﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xproject.Framework.Core;
using Xproject.Framework.Core.DataContext;
using Xproject.Framework.Core.Repositories;
using Model.Util;
using Repository.Util;
using Xproject.Util;
using System.Data;
using Model.Util.DTO;


namespace Services.Util
{
   public  class IndexCategoryServices : Service
    {
        IRepositoryAsync<IndexCategory> m_IndexCategoryRepository;
        IRepositoryAsync<UploadFile> m_UploadFileRepository;

        public IndexCategoryServices(IDataContextAsync dataContext) : base(dataContext)
        {
            m_IndexCategoryRepository = m_UnitOfWork.RepositoryAsync<IndexCategory>();
            m_UploadFileRepository = m_UnitOfWork.RepositoryAsync<UploadFile>();
        }

        private List<IndexCategory> GetAllIndexCategory()
        {
            if (Cache_IndexCategory.INDEXCATEGORY_KANGDAO == null)
            {
                Cache_IndexCategory.INDEXCATEGORY_KANGDAO = m_IndexCategoryRepository.GetIndexCategoryList();
            }
            return Cache_IndexCategory.INDEXCATEGORY_KANGDAO;
        }

        public   List<IndexCategory> GetTopNIndexCategorys(int N)
        {
            List<IndexCategory> allads = GetAllIndexCategory();
           
            return allads.Take(N).ToList();
        }


        public List<IndexCategory> GetAllIndexCategorys(int limit, int offset, string Name,  string remarks,
         DateTime? startTime, DateTime? endTime,  out int total)
        {
            List<IndexCategory> allads = GetAllIndexCategory();
            if (!string.IsNullOrEmpty(Name))
            {
                allads = allads.Where(i => i.IndexCateShowName.Contains(Name)).ToList();
            }           
            if (!string.IsNullOrEmpty(remarks))
            {
                allads = allads.Where(i => i.Remarks.Contains(remarks)).ToList();
            }
            if (startTime.HasValue)
            {
                allads = allads.Where(i => i.CreateTime >= startTime.Value).ToList();
            }
            if (endTime.HasValue)
            {
                allads = allads.Where(i => i.CreateTime <= endTime.Value).ToList();
            }
            total = allads.Count;
            return allads.Skip(offset).Take(limit).ToList();
        }

        public IndexCategory GetById( string Id)
        {
            return m_IndexCategoryRepository.Get(Id);
        }
        public bool EditIndexCategory(UploadFile up, IndexCategory indexCategory, string userid)
        {
            int result = 0;
            m_UnitOfWork.BeginTransaction();
            if (indexCategory != null)
            {
                indexCategory.EntityState = EntityState.Modified;
                indexCategory.CreateTime = DateTime.Now;
                m_IndexCategoryRepository.Update(indexCategory);
                result++;
            }
            if (up != null)
            {
                up.CreateTime = DateTime.Now;
                up.Creator = userid;
                up.EntityState = EntityState.Added;
                m_UploadFileRepository.Insert(up);
                result++;
            }
            if (m_UnitOfWork.SaveChanges() == result)
            {
                m_UnitOfWork.Commit();
                Cache_IndexCategory.ClearAd_INDEXCATEGORY_KANGDAO();
                return true;
            }
            else
            {
                m_UnitOfWork.Rollback();
                return false;
            }
        }

        public bool AddIndexCategory(UploadFile up, IndexCategory indexCategory, string userid)
        {
            int result = 0;
            m_UnitOfWork.BeginTransaction();
            if (indexCategory != null)
            {
                indexCategory.EntityState = EntityState.Added;
                indexCategory.CreateTime = DateTime.Now;
                m_IndexCategoryRepository.Insert(indexCategory);
                result++;
            }
            if (up != null)
            {
                up.CreateTime = DateTime.Now;
                up.Creator = userid;
                up.EntityState = EntityState.Added;
                m_UploadFileRepository.Insert(up);
                result++;
            }
            if (m_UnitOfWork.SaveChanges() == result)
            {
                m_UnitOfWork.Commit();
                Cache_IndexCategory.ClearAd_INDEXCATEGORY_KANGDAO();
                return true;
            }
            else
            {
                m_UnitOfWork.Rollback();
                return false;
            }
        }

        public bool DelIndexCategory(IndexCategory indexCategory)
        {
            if (indexCategory != null)
            {
                indexCategory.EntityState = EntityState.Deleted;
                m_IndexCategoryRepository.Delete(indexCategory);
                return m_UnitOfWork.SaveChanges() == 1;
            }
            return false;
        }
    }
}
